package com.uziot.bucket.boot.message.convert;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

/**
 * @author shidt
 * @version V1.0
 * @className FastJsonMessageConverterAdapter
 * @date 2021-07-30 10:15:31
 * @description
 */

public class FastJsonMessageConverterAdapter extends FastJsonHttpMessageConverter {
    @Override
    public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        String typeName = type.getTypeName();
        if ("java.lang.String".equals(typeName)) {
            return StreamUtils.copyToString(inputMessage.getBody(), Charset.defaultCharset());
        }
        return super.read(type, contextClass, inputMessage);
    }
}
